package com.example.demo3;

/**
 * @author 张成
 * @version 1.0
 * @date 2022/3/11 10:42
 */

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@WebServlet(name = "DownloadPicServlet", value = "/DownloadPicServlet")
public class DownloadPicServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("image/jpeg");
        OutputStream out = response.getOutputStream();
        InputStream inputStream = getServletContext().getResourceAsStream("/img0.jpg");
        byte[] b = new byte[1024];
        int n;
        while ((n = inputStream.read(b)) != -1) {
            out.write(b, 0, n);
        }
    }
}
